<?php
include_once('libs/class.database.php');

class User{

	public $link;

	function __construct()
	{
		$konek = new Database();
		$this->link = $konek->connect();
		return $this->link;
	}
	
	function createUser($username, $password)
	{
		$query=$this->link->prepare("INSERT INTO user(username,password,date_created) VALUES(?,?,?)");
		$date_created=date("Y-m-d");
		$password_md5=md5($password);
		$isi=array($username,$password_md5,$date_created);
		$query->execute($isi);
		$jml_data=$query->rowCount();
		return$jml_data;
	}

	function getUser($username)
	{
		$query=$this->link->query("SELECT * FROM user WHERE username='$username'");
		$jml_data=$query->rowCount();
		if($jml_data==1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getAllUser()
	{
		$query=$this->link->query("SELECT a.id id,a.username username,a.id_level level,b.nama nama,b.email email FROM user a RIGHT JOIN biodata b ON a.id=b.id_username");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function updateBanned($id)
	{
		$date_updated=date("Y-m-d");
		$query=$this->link->query("UPDATE user SET stat=1, date_updated=$date_updated WHERE id=$id");
		$jml_data=$query->rowCount();
		return $jml_data;
	}
	
	function updateUser($id,$password)
	{
		$date_updated=date("Y-m-d");
		$password_md5=md5($password);
		$query=$this->link->query("UPDATE user SET password='$password_md5', date_updated=$date_updated WHERE id=$id");
		$jml_data=$query->rowCount();
		return $jml_data;
	}
	
	function loginUser($username,$password)
	{
		$password_md5=md5($password);
		$query=$this->link->query("SELECT * FROM user WHERE username='$username' AND password='$password_md5' AND stat=0");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
	function deleteUser($id)
	{
		$query=$this->link->query("DELETE FROM user WHERE id='$id'");
		$jml_data=$query->rowCount();
		return $jml_data;
	}
}
